const path = require('path')
const resolve = dir => path.join(__dirname, dir)
const bodyParser = require('body-parser')
const proxy = require('http-proxy-middleware')


const mockData = require('./mock/data.json')
module.exports = {
    publicPath: './',
    devServer: {
        disableHostCheck: true,
        proxy: {
            '/api/': {
                //要访问的跨域的域名
                target: process.env.VUE_APP_API_HOST,
                ws: true, // 是否启用websockets
                secure: false, // 使用的是http协议则设置为false，https协议则设置为true
                //开启代理：在本地会创建一个虚拟服务端，然后发送请求的数据，并同时接收请求的数据，这样客户端端和服务端进行数据的交互就不会有跨域问题
                changeOrigin: true
            }

        },
        before(app) {
            let host = process.env.VUE_APP_API_HOST;
            if (host && (host.indexOf("localhost") > -1 || host.indexOf("127.0.0.1") > -1)) {
                app.use(bodyParser.urlencoded({extended: false}));
                app.use(bodyParser.json())

                app.get('/api/getRobotSkin.htm', (req, res, next) => {
                    res.json(mockData["getRobotSkin"]);
                })
                app.get('/api/getRobotInfo.htm', (req, res, next) => {
                    res.json(mockData["getRobotInfo"]);
                })
                app.get('/api/getToken.htm', (req, res, next) => {
                    res.json(mockData["getToken"]);
                })
                app.get('/api/getUserInfo.htm', (req, res, next) => {
                    res.json(mockData["getUserInfo"]);
                })
                app.get('/api/getUserAuth.htm', (req, res, next) => {
                    res.json(mockData["getUserAuth"]);
                })
                app.get('/api/getGreetings.htm', (req, res, next) => {
                    res.json(mockData["getGreetings"]);
                })
                // app.post('/api/addRecord.htm', (req, res, next) => {
                //     res.json(mockData["addRecord"]);
                // })
                app.post('/api/comment.htm', (req, res, next) => {
                    res.json(mockData["comment"]);
                })
                app.post('/api/addSatisfaction.htm', (req, res, next) => {
                    res.json(mockData["addSatisfaction"]);
                })
                app.get('/api/getHotQuestions.htm', (req, res, next) => {
                    res.json(mockData["getHotQuestions"]);
                })
                app.get('/api/getSearchQuestions.htm', (req, res, next) => {
                    res.json(mockData["getHotQuestions"]);
                })
                app.post('/api/getTalkAnswers.htm', (req, res, next) => {
                    let type = req.body.type
                    res.json(mockData["getTalkAnswers_" + type]);
                })
            }
        }
    },
    outputDir: process.env.outputDir, // 打包生成目录
    lintOnSave: false,
    runtimeCompiler: true, // 是否使用包含运行时编译器的 Vue 构建版本
    productionSourceMap: false, // 生产环境的 source map
    chainWebpack: (config) => {
        config.resolve.alias
            .set('@', resolve('./src'))
            .set('components', resolve('./src/components'))
    }
}
